<template>
  <div v-loading="loading" class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('费用分摊规则') }}</span>
    </div>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-table :data="form.ruleList" border class="nowrapTable formTable">
        <el-table-column :label="$t('费用类型')" align="center">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('费用类型') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'ruleList.' + scope.$index + '.feeType'" :rules="rules.feeType">
              <dict-select
                v-model="scope.row.feeType"
                dictionary="fund_expense_type_fee"
                :placeholder="$t('费用类型')"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('费用分摊规则')" align="center">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('费用分摊规则') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'ruleList.' + scope.$index + '.feeRule'" :rules="rules.feeRule">
              <dict-select
                v-model="scope.row.feeRule"
                dictionary="fund_share_way"
                :placeholder="$t('费用分摊规则')"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('备注')" align="center">
          <template slot="header" slot-scope="{}">
            <span>{{ $t('备注') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'ruleList.' + scope.$index + '.remark'" :rules="rules.remark">
              <el-input v-model="scope.row.remark" type="textarea" class="fileTextarea" :placeholder="$t('备注')" show-word-limit maxlength="1000" />
            </el-form-item>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right; margin-top: 10px;">
      <!--保存 取消按钮-->
      <save-btn has-permi="feesharingrule:config" :cancel-show="false" />
    </div>
  </div>
</template>

<script>
import { saveFeeSharingRuleConfig, getData } from '@/api/fund/feesharingrule/feeSharingRuleConfig'
export default {
  data() {
    return {
      // 表单参数
      form: {
        ruleList: []
      },
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        feeType: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        feeRule: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
      },
      queryForm: {
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    // 初始化数据
    init() {
      this.loading = true
      getData().then(response => {
        this.form = response.data
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        ruleList: []
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveFeeSharingRuleConfig(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    }

  }

}
</script>
